package com.example.wendao.view.viewholder;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.common.ViewHolderBind;
import com.example.wendao.model.entity.Forum;
import com.example.wendao.model.entity.NewsComment;
import com.example.wendao.model.entity.User;
import com.example.wendao.model.service.ForumService;
import com.example.wendao.model.service.UserService;

public class MessageForumViewHolder extends RecyclerView.ViewHolder implements ViewHolderBind<NewsComment> {

    private TextView messagelist;
    private RelativeLayout layout;
    private int forumid;
    private Forum forum;
    private User user;
    private  String str;
    private SpannableStringBuilder style;
    public MessageForumViewHolder(@NonNull ViewGroup viewGroup) {
        super(PageUtils.inflateItemView(viewGroup, R.layout.fragment_listmessage));
        messagelist=itemView.findViewById(R.id.messagelist);
    }

    @Override
    public void bindTo(NewsComment newsComment) {
        forumid=newsComment.getFatherId();
        ForumService fs = new ForumService();
        forum =fs.findForumById(forumid);
        String title = forum.getForumTitle().substring(0,10)+"...";
        UserService us = new UserService();
        user= us.findUserById(forum.getForumAuthorId());
        String username = user.getUsername().substring(0,5)+"...";
        str=username+"在您所发布的帖子"+title+"下发表了评论";
        //给username和title加上字体颜色
        int bstart=str.indexOf(username);
        int bend=bstart+username.length();
        int fstart=str.indexOf(title);
        int fend=fstart+title.length();
        style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(Color.RED),bstart,bend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        messagelist.setText(str);
        /*跳转
            layout.setOnClickListener(v->{
            Bundle bundle=new Bundle();
            bundle.putInt("forumid",news.getNewsId());
            Navigation.findNavController(v).navigate(R.id.fragment_new,bundle);
        });*/
    }
}
